<template>
	<view class="muen_box">
		<view class="title_tip">工作台</view>
		<view class="muen_list">
			<view
				class="item"
				v-for="(item, index) in iconList"
				:key="index"
				@click="onClickPages(item.icon)"
			>
				<uni-badge
					v-if="item.icon == 13"
					class="uni-badge-left-margin"
					:text="num"
					absolute="rightTop"
					size="small"
				>
					<image
						:src="`/static/network/home/icon${item.icon}.svg`"
						lazy-load
						mode="aspectFit"
						class="icon"
					></image>
				</uni-badge>
				<image
					v-else
					:src="`/static/network/home/icon${item.icon}.svg`"
					lazy-load
					mode="aspectFit"
					class="icon"
				></image>
				<view class="title">
					{{ item.title }}
				</view>
			</view>
		</view>
		<wayType
			@set-popup="showBottom = $event"
			ref="wayTypeRef"
			@value-change="addWorkor"
			:showpopup="showBottom"
		></wayType>
	</view>
</template>

<script setup lang="ts">
	import {ref} from 'vue';
	import {onShow, onUnload, onLaunch, onLoad} from '@dcloudio/uni-app';
	import apiAccount from '@/util/network_api/account.js';
	import store from '@/store_network/index.js';
	import wayType from '@/components/u-network/wayType.vue';
	import apiNetwork from '@/util/network_api/network.js';
	import {moduleId as fixedModuleId} from '@/config/order';

	const wayTypeRef = ref();
	const showBottom = ref(false); //申请工单弹窗
	const iconList = ref([
		// { title: '创建服务商', icon: 0 },
		// { title: '加入服务商', icon: 1 },
		// { title: '认证中心', icon: 2 },

		{title: '新建工单', icon: 11},
		// {title: '真伪查询', icon: 10},
		{title: '整机返厂', icon: 15},
		// { title: '邀请成员', icon: 7 },
		// { title: '工单报表', icon: 4 },

		{title: '配件申请', icon: 9},
		// {title: '网点仓库', icon: 5},
		// { title: '工单结算', icon: 14 },
		// { title: '我的账户', icon: 6 },
		// {title: '工单结算', icon: 6},
		// {title: '工单统计', icon: 3},
		{title: '仓库明细', icon: 8},
		{title: '知识库', icon: 12},
		// {title: '公告', icon: 13},
	]);
	const iconListAll = ref([
		// { title: '创建服务商', icon: 0 },
		// { title: '加入服务商', icon: 1 },
		// { title: '认证中心', icon: 2 },

		{title: '新建工单', icon: 11},
		// {title: '真伪查询', icon: 10},
		{title: '整机返厂', icon: 15},
		// { title: '邀请成员', icon: 7 },
		// { title: '工单报表', icon: 4 },

		{title: '配件申请', icon: 9},
		// {title: '网点仓库', icon: 5},
		// { title: '工单结算', icon: 14 },
		// { title: '我的账户', icon: 6 },
		// {title: '工单结算', icon: 6},
		// {title: '工单统计', icon: 3},
		{title: '仓库明细', icon: 8},
		{title: '知识库', icon: 12},
		// {title: '公告', icon: 13},
	]);
	onLoad(async () => {
		// 获取用户信息
		await store.dispatch('getUpdataUser');

		if (!store.state.isManager) {
			iconList.value = iconListAll.value.filter(
				(o) => !(o.icon == 7 || o.icon == 5),
			);
		} else {
			iconList.value = iconListAll.value;
		}
		apiAccount.getWithdrawRoot().then((res) => {
			if (res.code == 20000) {
				if (!res.data.row) {
					iconList.value = iconList.value.filter((o) => o.icon != 6);
				}
			}
		});
		getLookSet();
		// getNum();
	});

	// 获取公告未读数量
	const num = ref(0);
	const getNum = () => {
		apiNetwork.getMobileSysNoticeUnreadNum().then((res) => {
			if (res.code == 20000) {
				num.value = res.data.count;
			}
		});
	};

	const onClickPages = async (index) => {
		switch (index) {
			case 0:
				uni.navigateTo({
					url: '/pages_network/user/create_network',
				});
				break;
			case 1:
				uni.navigateTo({
					url: '/pages_network/user/add_network',
				});
				break;
			// case 2:

			// 	break;
			case 3:
				uni.navigateTo({
					url: '/pages_network/home/ordertotal',
				});
				break;
			case 4:
				uni.navigateTo({
					url: '/pages_network/home/workOrder',
				});
				break;
			case 5:
				uni.navigateTo({
					url: '/pages_network/home/oneStore',
				});
				break;
			case 6:
				// uni.showToast({
				// 	title: "正在维护中，敬请期待...",
				// 	duration: 800,
				// 	icon: "none"
				// })
				uni.navigateTo({
					url: '/pages_network/home/myAccount/index',
				});
				break;
			case 7:
				uni.navigateTo({
					url: '/pages_network/home/addEmp',
				});
				break;
			case 8:
				uni.navigateTo({
					url: '/pages_network/home/storeContent/storeContent',
				});
				break;
			case 9:
				uni.navigateTo({
					url: '/pages_network/home/parts/partsList',
				});
				break;
			case 10:
				uni.navigateTo({
					url: '/pages_network/home/truePage/truthSearch',
				});
				break;
			case 11:
				await wayTypeRef.value.getList(0);
				showBottom.value = true;
				break;
			case 12:
				uni.navigateTo({
					url: '/pages_network/know/know',
				});
				break;
			case 13:
				uni.navigateTo({
					url: '/pages_network/notification/notification',
				});
				break;
			case 15:
				store.commit('setrepairGoodsVos', []);
				uni.navigateTo({
					url: `/pages_network/return_factory/return_factory`,
				});
				break;
			default:
				uni.showToast({
					title: '正在维护中，敬请期待...',
					duration: 800,
					icon: 'none',
				});
				break;
		}
	};
	// 选择申请方式
	const addWorkor = (item) => {
		store.commit('setWayType', {
			id: item.id,
			name: item.name,
			type: item.type, //1安装
			factoryId: '',
		});
		showBottom.value = false;
		store.commit('setorderGoodsTypes', []);
		store.commit('setchackFaultList', []);
		store.commit('setchackFaultItem', {id: null});
		store.commit('setcheckBrandItem', {id: null});
		store.commit('setcheckClassifyItem', {id: null});
		store.commit('setappointmentTime', null);
		store.commit('setUserOrder', {
			address: '', //客户详细地址
			area: '', //地址-县/区
			city: '', //地址-市
			helpOtherCreateOrder: false, //	是否代他人建单：0-否，1-是
			name: '', //客户姓名
			otherPhone: '', //其他电话
			phone: '', //客户手机号
			province: '', //地址-省
			town: '', //地址-镇
			userId: null,
		});
		if (item.id === fixedModuleId.engineerMachine) {
			uni.navigateTo({
				url: '/pages_network/home/addOrder/engineerMachine/add',
			});
			return;
		}
		uni.navigateTo({
			url: '/pages_network/home/addOrder/add',
		});
	};
	const getLookSet = () => {
		apiNetwork.getNetworkMobileOperateRoot().then((res) => {
			if (res.code == 20000) {
				if (res.data.row) {
					// 真伪查询
					if (!res.data.row.allowSecurityCheck) {
						iconList.value = iconList.value.filter((o) => o.icon != 10);
					}
					// 新建工单
					if (!res.data.row.allowCreateOrder) {
						iconList.value = iconList.value.filter((o) => o.icon != 11);
					}
					// 配件申请
					if (!res.data.row.allowApplySpare) {
						iconList.value = iconList.value.filter((o) => o.icon != 9);
					}
					if (!res.data.row.allowSelectKnowledgeBase) {
						iconList.value = iconList.value.filter((o) => o.icon != 12);
					}
				}
			}
		});
	};
	defineExpose({
		getLookSet,
	});
</script>

<style lang="scss" scoped>
	::v-deep(.uni-badge) {
		z-index: 1;
	}
	.muen_box {
		margin: 0 32rpx;
		background-color: #fff;
		padding-bottom: 30rpx;
		border-radius: 10rpx;
		border: 1rpx solid #eee;

		.title_tip {
			padding: 24rpx 0;
			margin: 0 30rpx;
			border-bottom: solid 1px #eee;
			font-size: 28rpx;
			font-weight: bold;
		}

		.muen_list {
			display: flex;
			flex-wrap: wrap;
			text-align: center;

			.item {
				width: 25%;
				margin-top: 32rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.title {
					margin-top: 12rpx;
					font-size: 26rpx;
				}

				.icon {
					// font-size: 32rpx;
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}
			}
		}
	}
</style>
